﻿
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
namespace KinonekoSoftware.Foundation.Markups
{
    public class BrushExtension : MarkupExtension
    {
        public BrushExtension()
        {
            Opacity = 1d;
        }

        public BrushExtension(BrushKey theme)
        {
            Theme   = theme;
            Opacity = 1d;
        }

        public BrushExtension(BrushKey theme, double opacity)
        {
            Theme   = theme;
            Opacity = Math.Clamp(opacity, 0d, 1d);
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            ThemeSystem.GetARGB(Xplat.Theme.Colors[Theme], out var a, out var r, out var g, out var b);

#if WINDOWS
            return new SolidColorBrush
            {
                Color   = Color.FromArgb(a, r, g, b),
                Opacity = Opacity,
            };
#else
            return new SolidColorBrush
            {
                Color = new Color(a, r, g, b),
                Opacity = Opacity,
            };

#endif
        }

        public BrushKey Theme   { get; set; }
        public double   Opacity { get; set; }
    }

    public class BackgroundBrushExtension : MarkupExtension
    {
        public BackgroundBrushExtension() : this(3)
        {
        }

        public BackgroundBrushExtension(int level) => Level = level;

        // MarkupExtension 是解析时的操作，不能动态应用
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var level = Math.Clamp(Level, 1, 6);
            var c = level switch
            {
                1 => BrushKey.BackgroundLevel1,
                2 => BrushKey.BackgroundLevel2,
                4 => BrushKey.BackgroundLevel4,
                5 => BrushKey.BackgroundLevel5,
                6 => BrushKey.BackgroundLevel6,
                _ => BrushKey.BackgroundLevel3,
            };
            
            ThemeSystem.GetARGB(Xplat.Theme.Colors[c], out var a, out var r, out var g, out var b);

#if WINDOWS
            return new SolidColorBrush
            {
                Color   = Color.FromArgb(a, r, g, b),
            };
#else
            return new SolidColorBrush
            {
                Color = new Color(a, r, g, b),
            };

#endif
        }

        public int Level { get; set; }
    }

    public class ForegroundBrushExtension : MarkupExtension
    {
        public ForegroundBrushExtension() {}
        public ForegroundBrushExtension(int level) => Level = level;

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var level = Math.Clamp(Level, 1, 3);
            var c = level switch
            {
                3 => BrushKey.ForegroundLevel3,
                2 => BrushKey.ForegroundLevel2,
                _ => BrushKey.ForegroundLevel1,
            };
            
            ThemeSystem.GetARGB(Xplat.Theme.Colors[c], out var a, out var r, out var g, out var b);

#if WINDOWS
            return new SolidColorBrush
            {
                Color   = Color.FromArgb(a, r, g, b),
            };
#else
            return new SolidColorBrush
            {
                Color = new Color(a, r, g, b),
            };

#endif
        }

        public int Level { get; set; }
    }
}